package cn.longg.controller;

import cn.longg.entity.Article;
import cn.longg.entity.Favorite;
import cn.longg.entity.Result;
import cn.longg.entity.User;
import cn.longg.service.IArticleService;
import cn.longg.service.IFavoriteService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("main/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @Autowired
    private IFavoriteService favoriteService;

    @RequestMapping("toArticle")
    public ModelAndView toArticle(){
        return new ModelAndView("article");
    }

    @RequestMapping("selectByPage/{page}/{pageSize}")
    public Result selectByPage(@PathVariable int page,@PathVariable int pageSize, @RequestBody Article article){
        PageInfo pageInfo = articleService.selectByPage(page,pageSize,article);
        return new Result(true,"查询成功",pageInfo);
    }

    @RequestMapping("toAddArticle")
    public ModelAndView toAddArticle(){
        return new ModelAndView("article_add");
    }

    @RequestMapping("addArticle")
    public Result addArticle(@RequestBody Article article, HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        article.setPublishDate(new Date());
        article.setPublishRealName(loginUser.getRealName());
        article.setUserId(loginUser.getId());
        article.setBrowseCount(0);
        int i = articleService.insertSelective(article);
        if(0 < i){
            return new Result(true,"发布成功",null);
        }
        return new Result(false,"发布失败",null);
    }

    @RequestMapping("toArticleDetail")
    public ModelAndView toArticleDetail(){
        return new ModelAndView("article_detail");
    }

    @RequestMapping("findfavouriteNum")
    public Result findfavouriteNum(@RequestBody Article article){
        List<Favorite> favorites = articleService.findfavouriteNum(article);
        return new Result(true,"查询成功",favorites.size());
    }

    @RequestMapping("browseIncr")
    public Result browseIncr(@RequestBody Article article){
        int i = articleService.browseIncr(article);
        return new Result(0 < i,"",null);
    }

    @RequestMapping("whoFavorite/{articleId}")
    public Result whoFavorite(@PathVariable int articleId, HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        Map<String,Object> map = articleService.whoFavorite(articleId,loginUser);
        return new Result(true,"成功",map);

    }

    @RequestMapping("changeFav/{articleId}")
    public Result changeFav(@PathVariable int articleId,HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        Favorite favorite = new Favorite();
        favorite.setaId(articleId);
        favorite.setuId(loginUser.getId());
        Favorite favorite1 = favoriteService.selectOne(favorite);
        if(null != favorite1){
            int delete = favoriteService.delete(favorite);
            return new Result(delete > 0,"",null);
        }else{
            int insert = favoriteService.insertSelective(favorite);
            return new Result(insert > 0,"",null);
        }
    }

    @RequestMapping("toArticleColl")
    public ModelAndView toArticleColl(){
        return new ModelAndView("article_collect");
    }

    @RequestMapping("selectFavByPage/{page}/{pageSize}")
    public Result selectFavByPage(@PathVariable int page,@PathVariable int pageSize,@RequestBody Article article,HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");

        PageInfo pageInfo = articleService.selectFavByPage(page,pageSize,article.getTitle(),loginUser.getId());
        return new Result(true,"查询成功",pageInfo);

    }
}
